//
//  ViewController2.m
//  someDemo
//
//  Created by 上官惠阳 on 2016/11/30.
//  Copyright © 2016年 上官惠阳. All rights reserved.
//

#import "ViewController2.h"
#import "ViewController5.h"
#import "UITextField+Add.h"

@interface ViewController2 ()<UIScrollViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *ImageView2;

@end

@implementation ViewController2
{
    UIScrollView *_scrollView;
    UIPageControl *_pageCtrl;
}
//初始化对象，初始化数据
- (instancetype)init
{
    self = [super init];
    if (self) {
        NSString *currentClassName = [NSString stringWithUTF8String:object_getClassName(self)];
        NSLog(@"%@-----%s",currentClassName,__FUNCTION__);
    }
    return self;
}
//从nib载入视图 ，通常这一步不需要去干涉。除非你没有使用xib文件创建视图
//- (void)loadView
//{
//    NSString *currentClassName = [NSString stringWithUTF8String:object_getClassName(self)];
//    NSLog(@"%@-----%s",currentClassName,__FUNCTION__);
//}
//载入完成，可以进行自定义数据以及动态创建其他控件
- (void)viewDidLoad {
    [super viewDidLoad];

    //[self createImageView];

    //[self createImageView2];

    //[self createScrollView];
    //[self createScrollView2];
    
    [self setupForDismissKeyboard];
}
//视图将出现在屏幕之前，马上这个视图就会被展现在屏幕上了
- (void)viewWillAppear:(BOOL)animated
{
    NSString *currentClassName = [NSString stringWithUTF8String:object_getClassName(self)];
    NSLog(@"%@-----%s",currentClassName,__FUNCTION__);
}
//视图已在屏幕上渲染完成
- (void)viewDidAppear:(BOOL)animated
{
    NSString *currentClassName = [NSString stringWithUTF8String:object_getClassName(self)];
    NSLog(@"%@-----%s",currentClassName,__FUNCTION__);
}
//视图将被从屏幕上移除之前执行
- (void)viewWillDisappear:(BOOL)animated
{
    NSString *currentClassName = [NSString stringWithUTF8String:object_getClassName(self)];
    NSLog(@"%@-----%s",currentClassName,__FUNCTION__);
}
//视图已经被从屏幕上移除，用户看不到这个视图了
- (void)viewDidDisappear:(BOOL)animated
{
    NSString *currentClassName = [NSString stringWithUTF8String:object_getClassName(self)];
    NSLog(@"%@-----%s",currentClassName,__FUNCTION__);
}
//视图被销毁，此处需要对你在init和viewDidLoad中创建的对象进行释放
- (void)dealloc
{
    NSString *currentClassName = [NSString stringWithUTF8String:object_getClassName(self)];
    NSLog(@"%@-----%s",currentClassName,__FUNCTION__);
}
#pragma mark createSomeView
- (void)createImageView2
{
    _ImageView2.layer.masksToBounds = YES;
    _ImageView2.layer.cornerRadius = 10;
    _ImageView2.layer.borderColor = [UIColor blueColor].CGColor;
    _ImageView2.layer.borderWidth = 2;
    UIImage *img1 = [UIImage imageNamed:@"che.png"];

    UIImage *img2 = [UIImage imageNamed:@"che1.png"];

    UIImage *img3 = [UIImage imageNamed:@"che2.png"];

    NSArray *imagesArray = @[img1,img2,img3];

    _ImageView2.animationImages = imagesArray;
    _ImageView2.animationDuration = [imagesArray count];
    _ImageView2.animationRepeatCount = 0;
    [_ImageView2 startAnimating];

    UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageView2:)];
    [_ImageView2 addGestureRecognizer:singleTap];
}
- (void)tapImageView2:(UITapGestureRecognizer *)tap
{
    UIImageView *ImageView2 = (UIImageView *)tap.view;
    if (ImageView2.isAnimating){
        [ImageView2 stopAnimating];
    }else{
        [ImageView2 startAnimating];
    }
}
- (void)createImageView
{
    //初始化
    UIImageView  *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 120, 120)];
    imageView.backgroundColor = [UIColor blueColor];

    [self.view addSubview:imageView];

    //需要设置图片 UIImage
    //第一种：
    [imageView setImage:[UIImage imageNamed:@"che.png"]];

    //第二种：
    NSString *filePath=[[NSBundle mainBundle] pathForResource:@"che" ofType:@"png"];
    UIImage *images=[UIImage imageWithContentsOfFile:filePath];
    //[imageView setImage:images];

    //第三种：
    NSData *data=[NSData dataWithContentsOfFile:filePath];
    UIImage *image2=[UIImage imageWithData:data];
//    [imageView setImage:image2];

    //(2)设置圆角
    imageView.layer.masksToBounds = YES;
    imageView.layer.cornerRadius = 10;

    //(3)设置边框颜色和大小
    imageView.layer.borderColor = [UIColor orangeColor].CGColor;
    imageView.layer.borderWidth = 2;

    
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    //(4)contentMode属性：当图片小于imageView的大小处理图片显示
    /*
     UIViewContentModeScaleToFill,填充整个imageView
     UIViewContentModeScaleAspectFit,按照图片比例显示
     UIViewContentModeScaleAspectFill按照图片比例填充整个imageView
     UIViewContentModeLeft
     */
    imageView.contentMode = UIViewContentModeScaleAspectFit;

    //(5)播放一系列图片
    UIImage *img1 = [UIImage imageNamed:@"che.png"];

    UIImage *img2 = [UIImage imageNamed:@"che1.png"];

    UIImage *img3 = [UIImage imageNamed:@"che2.png"];

    NSArray *imagesArray = @[img1,img2,img3];

    imageView.animationImages = imagesArray;
    // 设定所有的图片在多少秒内播放完毕

    imageView.animationDuration = [imagesArray count];

    // 不重复播放多少遍，0表示无数遍

    imageView.animationRepeatCount = 0;

    // 开始播放
    [imageView startAnimating];
    
    
    //(6)为图片添加单击事件：一定要先将userInteractionEnabled置为YES，这样才能响应单击事件
    imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
    [imageView addGestureRecognizer:singleTap];
}
//图片的点击事件
- (void)tapImageView:(UITapGestureRecognizer *)tap
{
    UIImageView *imageView = (UIImageView *)tap.view;
    if (imageView.isAnimating) {
        [imageView stopAnimating];
    }else{
        [imageView startAnimating];
    }
}
- (void)createScrollView2
{
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 10,screenWidth-40,300)];
    _scrollView = scrollView;
    scrollView.backgroundColor = [UIColor greenColor];
    scrollView.delegate = self;
    [self.view addSubview:(scrollView)];
    CGFloat scrollWidth = scrollView.frame.size.width;
    CGFloat scrollheigth = scrollView.frame.size.height;
    NSArray *imageNameArr = @[@"che.png",@"scale_aspect.jpeg",@"che1.png"];
    for (int i = 0; i < imageNameArr.count; i ++) {
        NSString *imageName = imageNameArr[i];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView * imageView = [[UIImageView alloc] initWithImage:image];
        imageView.userInteractionEnabled = YES;
        imageView.frame = CGRectMake(i * scrollWidth, 0, scrollWidth, scrollheigth);
        [scrollView addSubview:imageView];
        scrollView.contentSize = CGSizeMake(scrollWidth*imageNameArr.count, scrollheigth);
        scrollView.bounces = YES;
        scrollView.pagingEnabled = YES;
        scrollView.scrollEnabled = YES;
        scrollView.showsHorizontalScrollIndicator = YES;
        scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
        UIPageControl *pageCtrl = [[UIPageControl alloc]initWithFrame:CGRectMake((screenWidth-100)/2, scrollheigth-40, 100, 30)];
        _pageCtrl = pageCtrl;
        pageCtrl.numberOfPages = imageNameArr.count;
        pageCtrl.currentPage = 0;
        pageCtrl.hidesForSinglePage = YES;
        pageCtrl.pageIndicatorTintColor = [UIColor blueColor];
        pageCtrl.currentPageIndicatorTintColor = [UIColor greenColor];
        [self.view addSubview:pageCtrl];
    }
}

- (void)createScrollView
{
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;

    //建立UIScrollView窗口，我们只打算用手机的上半屏显示图像，(这一步也可以在storyboard里完成)
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20,10, screenWidth - 40 , 300)];

    _scrollView = scrollView;

    scrollView.backgroundColor = [UIColor yellowColor];

    //设置代理
    scrollView.delegate = self;

    //把scrollView添加到视图结构中
    [self.view addSubview:scrollView];

    CGFloat scrollWidth = scrollView.frame.size.width;
    CGFloat scrollHeigth = scrollView.frame.size.height;

    //往scrollView添加图片控件
    NSArray *imageNameArr = @[@"scale_aspect.jpeg",@"che.png",@"che1.png",@"che2.png"];
    for (int i = 0; i < imageNameArr.count; i ++) {
        NSString *imageName = imageNameArr[i];
        //2.建立内容视图
        UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];

        imageView.userInteractionEnabled = YES;

//        imageView.frame = CGRectMake(i * scrollWidth, 0, scrollWidth, scrollHeigth);

        imageView.frame = CGRectMake(0, i * scrollHeigth, scrollWidth, scrollHeigth);

        //3.将内容视图作为scrollView的子视图
        [scrollView addSubview: imageView];
    }

    // 设置内容大小
    //这样设置可以横向滚动
//    scrollView.contentSize = CGSizeMake(scrollWidth*imageNameArr.count,scrollHeigth);

    //这样设置可以纵滚动
    scrollView.contentSize = CGSizeMake(scrollWidth,scrollHeigth*imageNameArr.count);

    //是否反弹
    scrollView.bounces = YES;

    //是否分页
    scrollView.pagingEnabled = YES;

    //是否滚动
    scrollView.scrollEnabled = YES;

    //是否显示横向滚动条
    scrollView.showsHorizontalScrollIndicator = YES;
    //是否显示纵向滚动条
    scrollView.showsVerticalScrollIndicator = YES;

    // 设置indicator风格
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;

    // 设置内容的边缘，默认为UIEdgeInsetsZero
//    scrollView.contentInset = UIEdgeInsetsMake(50, 50, 50, 50);//上，左，下，右

    /*********************************************************************/
    //创建UIPageControl
    UIPageControl *pageCtrl = [[UIPageControl alloc] initWithFrame:CGRectMake((screenWidth - 100)/2, scrollHeigth - 40, 100, 30)];

    _pageCtrl = pageCtrl;

    pageCtrl.numberOfPages = imageNameArr.count;//总的图片页数

    pageCtrl.currentPage = 0; //当前页

    //如果numberOfPages等于1的话是否隐藏pageCtrl
    pageCtrl.hidesForSinglePage = YES;

    //不是当前显示的页码圆点颜色
    pageCtrl.pageIndicatorTintColor = [UIColor orangeColor];

    //当前显示的页码圆点颜色
    pageCtrl.currentPageIndicatorTintColor = [UIColor blackColor];
    
    //将UIPageControl添加到scrollView上
    [self.view addSubview:pageCtrl];
}
#pragma mark scrollViewDelegate
// 是否支持滑动至顶部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
    return YES;
}

// 滑动到顶部时调用该方法
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidScrollToTop");
}

// scrollView 已经滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidScroll");
}

// scrollView 开始拖动
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewWillBeginDragging");
}

// scrollView 结束拖动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"scrollViewDidEndDragging");
}

// scrollView 开始减速（以下两个方法注意与以上两个方法加以区别）
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewWillBeginDecelerating");
}

// scrollview 减速停止
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidEndDecelerating");

    //更新UIPageControl的当前页
    CGPoint offset = scrollView.contentOffset;
   CGRect bounds = scrollView.frame;
   [_pageCtrl setCurrentPage:offset.x / bounds.size.width];
    
    //CGPoint offset = scrollView.contentOffset;
    //CGRect bounds = scrollView.frame;
    //[_pageCtrl setCurrentPage:offset.y /bounds.size.height];
}
#pragma mark createAlertController
//创建从底部弹出的
- (void)createAlertControllerActionSheet
{
    // 1.实例化
    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    // 2.实例化按钮
    UIAlertAction *alert1 = [UIAlertAction actionWithTitle:@"按钮1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        //点击按钮1要处理的事件

    }];
    UIAlertAction *alert2 = [UIAlertAction actionWithTitle:@"按钮2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        //点击按钮2要处理的事件

    }];
    UIAlertAction *cancleAlert = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
        //点击取消按钮要处理的事件

    }];

    //添加按钮
    [alertControl addAction:alert1];
    [alertControl addAction:alert2];
    [alertControl addAction:cancleAlert];

    // 3.显示alertController:presentViewController
    [self presentViewController:alertControl animated:YES completion:nil];
}
//创建从中间弹出的
- (void)createAlertControllerStyleAlert
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"标题信息" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        //点击确定按钮要处理的事件


    }];
    [alertController addAction:cancleAction];
    [alertController addAction:sureAction];

    [self presentViewController:alertController animated:YES completion:nil];
}
// 跳转到相机或相册页面
- (void)createImagePickerControllerWithSourceType:(UIImagePickerControllerSourceType)sourceType
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

    //UINavigationControllerDelegate, UIImagePickerControllerDelegate
    imagePickerController.delegate = self;

    imagePickerController.allowsEditing = YES;

    imagePickerController.sourceType = sourceType;
    /*
     typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
     UIImagePickerControllerSourceTypePhotoLibrary,  //图片列表
     UIImagePickerControllerSourceTypeCamera, //摄像头
     UIImagePickerControllerSourceTypeSavedPhotosAlbum //相机相册
     }
     */

    //导航栏背景颜色
    imagePickerController.navigationBar.barTintColor = [UIColor greenColor];
    //导航栏标题颜色
    [imagePickerController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    //导航栏按钮颜色
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

    [self presentViewController:imagePickerController animated:YES completion:^{}];
}
#pragma mark-UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //获取从相机照的照片或从相册中选择的图片
    UIImage *image= [info objectForKey:@"UIImagePickerControllerEditedImage"];

    //转成二进制
    NSData *imageData = UIImageJPEGRepresentation(image,0.5);

    __weak typeof(self) weakSelf = self;
    [self dismissViewControllerAnimated:YES completion:^{

    }];
}
- (IBAction)logoutAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)popAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)meansBtnAction:(id)sender {
    ViewController5 *vc5 = [self.storyboard instantiateViewControllerWithIdentifier:@"vc5"];
    [self.navigationController pushViewController:vc5 animated:YES];
}


- (IBAction)createAlert:(id)sender {
    [self createAlertControllerStyleAlert];
}

- (IBAction)createSheet:(id)sender {
    [self createAlertControllerActionSheet];
}

- (IBAction)gotoImagePicker:(id)sender {
    [self createImagePickerControllerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end
